﻿using System.Web.Mvc;
using Tamias.BasicCmsPortableArea.Messages;
using Tamias.BasicCmsPortableArea.Areas.CmsAdmin.ViewModels.ContentGroup;
using Tamias.BasicCmsPortableArea.Domain;

namespace Tamias.BasicCmsPortableArea.Areas.CmsAdmin.Controllers
{
    public class ContentGroupController : Controller
    {
        #region Constructor

        public ContentGroupController()
        {
            _contentGroupService = new ContentGroupService();
        }

        public ContentGroupController(IContentGroupService contentGroupService)
        {
            _contentGroupService = contentGroupService;
        }

        #endregion

        private IContentGroupService _contentGroupService;

        public ActionResult Index()
        {
            var groups = _contentGroupService.GetGroups();

            if (groups == null || groups.Count == 0)
            {
                return View("NoGroups");
            }
            else
            {
                var vm = new IndexViewModel();
                groups.ForEach(g => vm.Groups.Add(
                    new ContentGroupViewItem()
                    {
                        ContentGroupId = g.Id,
                        Name = g.Name
                    }));

                return View(vm);
            }
        }

        public ViewResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(CreateEditViewModel vm)
        {
            if (ModelState.IsValid)
            {
                InstatiateAndSaveGroup(vm);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        private void InstatiateAndSaveGroup(CreateEditViewModel vm)
        {
            var group = new ContentGroup();
            group.Name = vm.Name;
            group.Id = vm.ContentGroupId;

            _contentGroupService.Save(group);
        }

        public ActionResult Edit(int? id)
        {
            if (!id.HasValue)
                return RedirectToAction("Index");

            var group = _contentGroupService.Get(id.Value);

            if (group == null)
                return RedirectToAction("Index");

            var vm = new CreateEditViewModel();
            vm.ContentGroupId = group.Id;
            vm.Name = group.Name;

            return View(vm);
        }

        [HttpPost]
        public ActionResult Edit(CreateEditViewModel vm)
        {
            if (ModelState.IsValid)
            {
                InstatiateAndSaveGroup(vm);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Delete(int? contentGroupId)
        {
            if (!contentGroupId.HasValue)
                return RedirectToAction("ErrorDeleting");

            _contentGroupService.Delete(contentGroupId.Value);
            return RedirectToAction("Index");
        }
    }
}
